
package cn.org.seventh.web.tag;

import java.net.URLEncoder;
import java.util.HashMap;

import javax.servlet.jsp.JspException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * @jsp.tag name="pagerParam"
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Dec 3, 2007 10:55:21 AM
 */
public class PagerParameterTag extends BaseTag {

    private static final long serialVersionUID = -2939284022007264906L;

    private final transient Log log =
        LogFactory.getLog(PagerParameterTag.class);

    protected String name;

    protected String value;

    /**
     * @param name
     * @jsp.attribute required = "false"
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param value
     * @jsp.attribute required = "false" rtexprvalue="true"
     */
    public void setValue(String value) {
        try {
            this.value =
                (String) ExpressionEvaluatorManager.evaluate("value", value,
                    String.class, this, pageContext);
        } catch (JspException e) {
            log.debug(e);
        }
    }

    public int doStartTag() throws JspException {
        if (!(getParent() instanceof PagerTag)) {
            throw new JspException("Invalid usage of tag pagerParam!");
        }
        return super.doStartTag();
    }

    public int doEndTag() throws JspException {
        PagerTag pagerTag = (PagerTag) getParent();
        HashMap params =
            (HashMap) pageContext.getAttribute(String.valueOf(pagerTag
                .hashCode()));
        if (params == null) {
            params = new HashMap();
            pageContext.setAttribute(String.valueOf(pagerTag.hashCode()),
                params);
        }
        if (value != null) {
            try {
                value = URLEncoder.encode(value, "ISO-8859-1");
            } catch (Throwable t) {
            }
        }
        params.put(name, value);
        return super.doEndTag();
    }
}
